package com.kaifamiao.list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class ListIteratorTest1 {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.addAll( List.of("孙悟空","猪悟能","沙悟净","熊大","熊二","光头强") );
        System.out.println( list );

        Iterator<String> itor = list.iterator();
        while( itor.hasNext() ) {
            String name = itor.next();
            System.out.println( name );
            if( "光头强".equals( name ) ) { // if( name.equals( "光头强" ) ) {
                itor.remove();
            }
        }

        System.out.println( itor.hasNext() ); // false
        System.out.println( list );

        ListIterator<String> listItor = list.listIterator();
        while ( listItor.hasNext() ) {
            int i = listItor.nextIndex();
            String e = listItor.next();
            System.out.printf( "%d : %s\n", i, e );
        }

        System.out.println( listItor.hasNext() ); // false
        System.out.println( listItor.hasPrevious() ); // true

        while( listItor.hasPrevious() ){
            int i = listItor.previousIndex();
            String e = listItor.previous();
            System.out.printf( "%d : %s\n", i, e );
        }

        System.out.println( list );

        listItor = list.listIterator( 3 );
        while ( listItor.hasNext() ) {
            int i = listItor.nextIndex();
            String e = listItor.next();
            System.out.printf( "%d : %s\n", i, e );
        }

        System.out.println( "- ".repeat( 5 ) );

        while( listItor.hasPrevious() ){
            int i = listItor.previousIndex();
            String e = listItor.previous();
            System.out.printf( "%d : %s\n", i, e );
        }

    }

}
